<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/cookie.js"></script>
    <script src="../js/ajax.js"></script>
    <script src="../js/interface.js"></script>
    <style>
        .right {
            color: green;
            font-size: 12px;
        }

        .error {
            color: red;
            font-size: 12px;
        }
    </style>
</head>

<body>
    <div class="box">
        <p>
            <label for="user">用户名:</label>
            <input id="user" type="text">
            <span id="user_tips"></span>
        </p>
        <p>
            <label for="pwd">密&emsp;码:</label>
            <input id="pwd" type="password">
            <span id="pwd_tips"></span>
        </p>

        <p>
            <button id="btn">注册</button>
        </p>
    </div>
</body>
<script>


    // 1. 获取元素
    var userInp = document.getElementById("user");
    var userTips = document.getElementById("user_tips");

    var pwdInp = document.getElementById("pwd");
    var pwdTips = document.getElementById("pwd_tips");

    var btn = document.getElementById("btn");

    btn.onclick = function () {

        // debugger;
        // 用户输入的
        var user = userInp.value;
        var pwd = pwdInp.value;

        // var xhr = new XMLHttpRequest();

        // xhr.open("post", "../php/login.php", true);

        // xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        // xhr.send(`user=${user}&pwd=${pwd}`);

        // xhr.onreadystatechange = function () {
        //     if (xhr.readyState == 4 && xhr.status == 200) {
        //         console.log(xhr.responseText);
        //         var result = JSON.parse(xhr.responseText);
        //         var { status, detail } = result;
        //         if (status) {
        //             // alert("登录成功");
        //             setCookie("lgc", user, 7);

        //             var obj = urlParse();
        //             console.log(obj);
        //             if (obj.returnUrl) {   // 有returnUrl => 从其他页面跳转过来 (回去)
        //                 location.href = obj.returnUrl;
        //             } else {
        //                 location.href = "./index1.html";
        //             }


        //         } else {
        //             alert(detail);
        //         }

        //     }
        // }

        login({ user, pwd }).then(({ status, detail }) => {
            if (status) {
                setCookie("lgc", user, 7);
                var obj = urlParse();
                console.log(obj);
                if (obj.returnUrl) {   // 有returnUrl => 从其他页面跳转过来 (回去)
                    location.href = obj.returnUrl;
                } else {
                    location.href = "./index1.html";
                }
            } else {
                alert(detail);
            }
        }).catch(err => {
            throw err;
        })
    }






</script>

</html>